<template>
	<view v-if="iframeUrl" class="kefu-body env-bottom">
		<web-view :src="iframeUrl"></web-view>
	</view>
</template>

<script>
	// #ifndef H5
	import http from '@/common/http.js'
	let domain = http.API_URL('').split('/')
	const api_host = domain[2].replace(/http(s)?:\/\//g, '') // 不加https://
	// #endif
	// #ifdef H5
	const api_host = window.location.host
	// #endif
	const kefu_host = 'https://ws.cyyvip.com/web/dist'
	export default {
		data() {
			return {
				msg_url: kefu_host + '/#/pages/msg/index?',
				iframeUrl: '',
				userInfo: null,
				goods_name: null,
				goods_price: null,
				goods_img: null,
				goods_id: null,
				goods_h5_url: null,
			};
		},
		onLoad(p) {
			this.userInfo = uni.getStorageSync("userInfo");
			if (!this.userInfo) {
				uni.redirectTo({
					url: "/login/login/login_page",
				});
				return;
			}
			if (this.stores.kefu_id == '') {
				this.$utils.toast('还没有客服在线哦~');
				setTimeout(() => {
					this.$utils.goBack();
				}, 2000)
				return;
			}
			if (this.userInfo.binding == this.stores.kefu_id) {
				this.$utils.toast('不能给自己对话哦～');
				setTimeout(() => {
					this.$utils.goBack();
				}, 2000)
				return;
			}
			if (p.goods_name && p.goods_price && p.goods_img && p.goods_id) {
				this.goods_name = p.goods_name;
				this.goods_price = p.goods_price;
				this.goods_img = p.goods_img;
				this.goods_id = p.goods_img;
				this.goods_h5_url = 'https://' + api_host + '/h5/#/goods/goods/goods?id=' + p.goods_id;
			}
			this.iframeUrl = this.iframe_url();
		},
		methods: {
			iframe_url() {
				let url = (
					this.msg_url +
					'host=' + api_host +
					'&mobile=' + this.userInfo.binding +
					'&nickname=' + this.userInfo.nickname +
					'&avatar=' + this.userInfo.avatar_url +
					'&to_mobile=' + this.stores.kefu_id
				);
				if (this.goods_id) {
					url += '&goods_name=' + this.goods_name;
					url += '&goods_price=' + this.goods_price;
					url += '&goods_img=' + this.goods_img;
					url += '&open_url=' + encodeURIComponent(this.goods_h5_url);
				}
        
        // #ifdef APP
        url += '&is_app=1';
        // #endif
				
				return url;
			}
		},
		computed: {},
	};
</script>

<style scoped>
	.kefu-body {
		width: 100vw;
		height: 100vh;
		/* #ifdef H5 */
		height: calc(100vh - 44px);
		/* #endif */
	}

	.env-bottom {
		padding-bottom: env(safe-area-inset-bottom);
	}
</style>